package com.ptszyxx.popose.module.main.mine;

import android.os.Bundle;

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.ptszyxx.popose.BR;
import com.ptszyxx.popose.R;
import com.ptszyxx.popose.databinding.FragmentMineTaskBinding;
import com.ptszyxx.popose.module.main.mine.adapter.MineTaskAdapter;
import com.ptszyxx.popose.module.main.mine.vm.MineTaskVM;
import com.ysg.base.BaseActivity;
import com.ysg.http.data.AppViewModelFactory;
import com.ysg.http.data.entity.mine.SignResult;
import com.ysg.http.data.entity.mine.TaskEntity;
import com.ysg.utils.YDialogUtil;
import com.ysg.utils.YRecyclerViewUtil;
import com.ysg.widget.dialog.SignDialog;

import java.util.List;

public class MineTaskFragment extends BaseActivity<FragmentMineTaskBinding, MineTaskVM> {
    private MineTaskAdapter newAdapter;
    private MineTaskAdapter dayAdapter;

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.fragment_mine_task;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public MineTaskVM initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(MineTaskFragment.this.getApplication());
        return ViewModelProviders.of(this, factory).get(MineTaskVM.class);
    }

    @Override
    public void initAdapter() {
        super.initAdapter();
        newAdapter = new MineTaskAdapter(viewModel);
        dayAdapter = new MineTaskAdapter(viewModel);
        YRecyclerViewUtil.init(MineTaskFragment.this, binding.recyclerViewNew, newAdapter);
        YRecyclerViewUtil.init(MineTaskFragment.this, binding.recyclerViewDay, dayAdapter);
    }

    @Override
    public void onResume() {
        super.onResume();
        viewModel.requestTaskList();
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.uc.onRefreshNewEvent.observe(this, new Observer<List<TaskEntity>>() {
            @Override
            public void onChanged(List<TaskEntity> list) {
                setAdapterNewUI(list);
            }
        });
        viewModel.uc.onRefreshDayEvent.observe(this, new Observer<List<TaskEntity>>() {
            @Override
            public void onChanged(List<TaskEntity> list) {
                setAdapterDayUI(list);
            }
        });
        viewModel.uc.onSignEvent.observe(this, o -> showDialogSign((SignResult) o));
    }

    private void setAdapterNewUI(List<TaskEntity> list) {
        newAdapter.setList(list);
    }
    private void setAdapterDayUI(List<TaskEntity> list) {
        dayAdapter.setList(list);
    }

    /**
     * 签到
     */
    private void showDialogSign(SignResult data) {
        YDialogUtil.getInstance().showSign(MineTaskFragment.this, data)
                .setOnSignListener(new SignDialog.OnSignListener() {
                    @Override
                    public void onSwitchClick() {
                        viewModel.requestSignTip();
                    }

                    @Override
                    public void onSignClick() {
                        viewModel.requestSignAdd();
                    }
                });
    }


}
